using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;
namespace Impact.Model.Objects
{
	/// <summary>
	/// Strongly-typed collection for the ArticleCategory class.
	/// </summary>
    [Serializable]
	public partial class ArticleCategoryCollection : ActiveList<ArticleCategory, ArticleCategoryCollection>
	{	   
		public ArticleCategoryCollection() {}
	}
	/// <summary>
	/// This is an ActiveRecord class which wraps the impact_ArticleCategories table.
	/// </summary>
	[Serializable]
	public partial class ArticleCategory : ActiveRecord<ArticleCategory>, IActiveRecord
	{
		#region .ctors and Default Settings
		
		public ArticleCategory()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}
		
		private void InitSetDefaults() { SetDefaults(); }
		
		public ArticleCategory(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}
        
		public ArticleCategory(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}
		 
		public ArticleCategory(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}
		
		protected static void SetSQLProps() { GetTableSchema(); }
		
		#endregion
		
		#region Schema and Query Accessor	
		public static Query CreateQuery() { return new Query(Schema); }
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}
		}
		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("impact_ArticleCategories", TableType.Table, DataService.GetInstance("ImpactProvider"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarCategoryId = new TableSchema.TableColumn(schema);
				colvarCategoryId.ColumnName = "CategoryId";
				colvarCategoryId.DataType = DbType.Int32;
				colvarCategoryId.MaxLength = 0;
				colvarCategoryId.AutoIncrement = true;
				colvarCategoryId.IsNullable = false;
				colvarCategoryId.IsPrimaryKey = true;
				colvarCategoryId.IsForeignKey = false;
				colvarCategoryId.IsReadOnly = false;
				colvarCategoryId.DefaultSetting = @"";
				colvarCategoryId.ForeignKeyTableName = "";
				schema.Columns.Add(colvarCategoryId);
				
				TableSchema.TableColumn colvarTemplateId = new TableSchema.TableColumn(schema);
				colvarTemplateId.ColumnName = "TemplateId";
				colvarTemplateId.DataType = DbType.Int32;
				colvarTemplateId.MaxLength = 0;
				colvarTemplateId.AutoIncrement = false;
				colvarTemplateId.IsNullable = true;
				colvarTemplateId.IsPrimaryKey = false;
				colvarTemplateId.IsForeignKey = true;
				colvarTemplateId.IsReadOnly = false;
				colvarTemplateId.DefaultSetting = @"";
				
					colvarTemplateId.ForeignKeyTableName = "impact_Templates";
				schema.Columns.Add(colvarTemplateId);
				
				TableSchema.TableColumn colvarNameKey = new TableSchema.TableColumn(schema);
				colvarNameKey.ColumnName = "NameKey";
				colvarNameKey.DataType = DbType.AnsiString;
				colvarNameKey.MaxLength = 50;
				colvarNameKey.AutoIncrement = false;
				colvarNameKey.IsNullable = true;
				colvarNameKey.IsPrimaryKey = false;
				colvarNameKey.IsForeignKey = false;
				colvarNameKey.IsReadOnly = false;
				colvarNameKey.DefaultSetting = @"";
				colvarNameKey.ForeignKeyTableName = "";
				schema.Columns.Add(colvarNameKey);
				
				TableSchema.TableColumn colvarName = new TableSchema.TableColumn(schema);
				colvarName.ColumnName = "Name";
				colvarName.DataType = DbType.AnsiString;
				colvarName.MaxLength = 50;
				colvarName.AutoIncrement = false;
				colvarName.IsNullable = false;
				colvarName.IsPrimaryKey = false;
				colvarName.IsForeignKey = false;
				colvarName.IsReadOnly = false;
				colvarName.DefaultSetting = @"";
				colvarName.ForeignKeyTableName = "";
				schema.Columns.Add(colvarName);
				
				TableSchema.TableColumn colvarDescription = new TableSchema.TableColumn(schema);
				colvarDescription.ColumnName = "Description";
				colvarDescription.DataType = DbType.AnsiString;
				colvarDescription.MaxLength = 2147483647;
				colvarDescription.AutoIncrement = false;
				colvarDescription.IsNullable = true;
				colvarDescription.IsPrimaryKey = false;
				colvarDescription.IsForeignKey = false;
				colvarDescription.IsReadOnly = false;
				colvarDescription.DefaultSetting = @"";
				colvarDescription.ForeignKeyTableName = "";
				schema.Columns.Add(colvarDescription);
				
				TableSchema.TableColumn colvarAllowComments = new TableSchema.TableColumn(schema);
				colvarAllowComments.ColumnName = "AllowComments";
				colvarAllowComments.DataType = DbType.Boolean;
				colvarAllowComments.MaxLength = 0;
				colvarAllowComments.AutoIncrement = false;
				colvarAllowComments.IsNullable = false;
				colvarAllowComments.IsPrimaryKey = false;
				colvarAllowComments.IsForeignKey = false;
				colvarAllowComments.IsReadOnly = false;
				
						colvarAllowComments.DefaultSetting = @"((0))";
				colvarAllowComments.ForeignKeyTableName = "";
				schema.Columns.Add(colvarAllowComments);
				
				TableSchema.TableColumn colvarAllowRss = new TableSchema.TableColumn(schema);
				colvarAllowRss.ColumnName = "AllowRss";
				colvarAllowRss.DataType = DbType.Boolean;
				colvarAllowRss.MaxLength = 0;
				colvarAllowRss.AutoIncrement = false;
				colvarAllowRss.IsNullable = false;
				colvarAllowRss.IsPrimaryKey = false;
				colvarAllowRss.IsForeignKey = false;
				colvarAllowRss.IsReadOnly = false;
				
						colvarAllowRss.DefaultSetting = @"((1))";
				colvarAllowRss.ForeignKeyTableName = "";
				schema.Columns.Add(colvarAllowRss);
				
				TableSchema.TableColumn colvarMenuVisible = new TableSchema.TableColumn(schema);
				colvarMenuVisible.ColumnName = "MenuVisible";
				colvarMenuVisible.DataType = DbType.Boolean;
				colvarMenuVisible.MaxLength = 0;
				colvarMenuVisible.AutoIncrement = false;
				colvarMenuVisible.IsNullable = false;
				colvarMenuVisible.IsPrimaryKey = false;
				colvarMenuVisible.IsForeignKey = false;
				colvarMenuVisible.IsReadOnly = false;
				
						colvarMenuVisible.DefaultSetting = @"((1))";
				colvarMenuVisible.ForeignKeyTableName = "";
				schema.Columns.Add(colvarMenuVisible);
				
				TableSchema.TableColumn colvarCreatedBy = new TableSchema.TableColumn(schema);
				colvarCreatedBy.ColumnName = "CreatedBy";
				colvarCreatedBy.DataType = DbType.String;
				colvarCreatedBy.MaxLength = 50;
				colvarCreatedBy.AutoIncrement = false;
				colvarCreatedBy.IsNullable = true;
				colvarCreatedBy.IsPrimaryKey = false;
				colvarCreatedBy.IsForeignKey = false;
				colvarCreatedBy.IsReadOnly = false;
				colvarCreatedBy.DefaultSetting = @"";
				colvarCreatedBy.ForeignKeyTableName = "";
				schema.Columns.Add(colvarCreatedBy);
				
				TableSchema.TableColumn colvarCreatedOn = new TableSchema.TableColumn(schema);
				colvarCreatedOn.ColumnName = "CreatedOn";
				colvarCreatedOn.DataType = DbType.DateTime;
				colvarCreatedOn.MaxLength = 0;
				colvarCreatedOn.AutoIncrement = false;
				colvarCreatedOn.IsNullable = true;
				colvarCreatedOn.IsPrimaryKey = false;
				colvarCreatedOn.IsForeignKey = false;
				colvarCreatedOn.IsReadOnly = false;
				colvarCreatedOn.DefaultSetting = @"";
				colvarCreatedOn.ForeignKeyTableName = "";
				schema.Columns.Add(colvarCreatedOn);
				
				TableSchema.TableColumn colvarModifiedBy = new TableSchema.TableColumn(schema);
				colvarModifiedBy.ColumnName = "ModifiedBy";
				colvarModifiedBy.DataType = DbType.String;
				colvarModifiedBy.MaxLength = 50;
				colvarModifiedBy.AutoIncrement = false;
				colvarModifiedBy.IsNullable = true;
				colvarModifiedBy.IsPrimaryKey = false;
				colvarModifiedBy.IsForeignKey = false;
				colvarModifiedBy.IsReadOnly = false;
				colvarModifiedBy.DefaultSetting = @"";
				colvarModifiedBy.ForeignKeyTableName = "";
				schema.Columns.Add(colvarModifiedBy);
				
				TableSchema.TableColumn colvarModifiedOn = new TableSchema.TableColumn(schema);
				colvarModifiedOn.ColumnName = "ModifiedOn";
				colvarModifiedOn.DataType = DbType.DateTime;
				colvarModifiedOn.MaxLength = 0;
				colvarModifiedOn.AutoIncrement = false;
				colvarModifiedOn.IsNullable = true;
				colvarModifiedOn.IsPrimaryKey = false;
				colvarModifiedOn.IsForeignKey = false;
				colvarModifiedOn.IsReadOnly = false;
				colvarModifiedOn.DefaultSetting = @"";
				colvarModifiedOn.ForeignKeyTableName = "";
				schema.Columns.Add(colvarModifiedOn);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["ImpactProvider"].AddSchema("impact_ArticleCategories",schema);
			}
		}
		#endregion
		
		#region Props
		  
		[XmlAttribute("CategoryId")]
		[Bindable(true)]
		public int CategoryId 
		{
			get { return GetColumnValue<int>(Columns.CategoryId); }
			set { SetColumnValue(Columns.CategoryId, value); }
		}
		  
		[XmlAttribute("TemplateId")]
		[Bindable(true)]
		public int? TemplateId 
		{
			get { return GetColumnValue<int?>(Columns.TemplateId); }
			set { SetColumnValue(Columns.TemplateId, value); }
		}
		  
		[XmlAttribute("NameKey")]
		[Bindable(true)]
		public string NameKey 
		{
			get { return GetColumnValue<string>(Columns.NameKey); }
			set { SetColumnValue(Columns.NameKey, value); }
		}
		  
		[XmlAttribute("Name")]
		[Bindable(true)]
		public string Name 
		{
			get { return GetColumnValue<string>(Columns.Name); }
			set { SetColumnValue(Columns.Name, value); }
		}
		  
		[XmlAttribute("Description")]
		[Bindable(true)]
		public string Description 
		{
			get { return GetColumnValue<string>(Columns.Description); }
			set { SetColumnValue(Columns.Description, value); }
		}
		  
		[XmlAttribute("AllowComments")]
		[Bindable(true)]
		public bool AllowComments 
		{
			get { return GetColumnValue<bool>(Columns.AllowComments); }
			set { SetColumnValue(Columns.AllowComments, value); }
		}
		  
		[XmlAttribute("AllowRss")]
		[Bindable(true)]
		public bool AllowRss 
		{
			get { return GetColumnValue<bool>(Columns.AllowRss); }
			set { SetColumnValue(Columns.AllowRss, value); }
		}
		  
		[XmlAttribute("MenuVisible")]
		[Bindable(true)]
		public bool MenuVisible 
		{
			get { return GetColumnValue<bool>(Columns.MenuVisible); }
			set { SetColumnValue(Columns.MenuVisible, value); }
		}
		  
		[XmlAttribute("CreatedBy")]
		[Bindable(true)]
		public string CreatedBy 
		{
			get { return GetColumnValue<string>(Columns.CreatedBy); }
			set { SetColumnValue(Columns.CreatedBy, value); }
		}
		  
		[XmlAttribute("CreatedOn")]
		[Bindable(true)]
		public DateTime? CreatedOn 
		{
			get { return GetColumnValue<DateTime?>(Columns.CreatedOn); }
			set { SetColumnValue(Columns.CreatedOn, value); }
		}
		  
		[XmlAttribute("ModifiedBy")]
		[Bindable(true)]
		public string ModifiedBy 
		{
			get { return GetColumnValue<string>(Columns.ModifiedBy); }
			set { SetColumnValue(Columns.ModifiedBy, value); }
		}
		  
		[XmlAttribute("ModifiedOn")]
		[Bindable(true)]
		public DateTime? ModifiedOn 
		{
			get { return GetColumnValue<DateTime?>(Columns.ModifiedOn); }
			set { SetColumnValue(Columns.ModifiedOn, value); }
		}
		
		#endregion
		
		
		#region PrimaryKey Methods		
		
        protected override void SetPrimaryKey(object oValue)
        {
            base.SetPrimaryKey(oValue);
            
            SetPKValues();
        }
        
				
		private Impact.Model.Objects.ArticleCollection colArticles;
		public Impact.Model.Objects.ArticleCollection Articles
		{
			get
			{
				if(colArticles == null)
				{
					colArticles = new Impact.Model.Objects.ArticleCollection().Where(Article.Columns.CategoryId, CategoryId).Load();
					colArticles.ListChanged += new ListChangedEventHandler(colArticles_ListChanged);
				}
				return colArticles;			
			}
			set 
			{ 
					colArticles = value; 
					colArticles.ListChanged += new ListChangedEventHandler(colArticles_ListChanged);
			}
		}
		
		void colArticles_ListChanged(object sender, ListChangedEventArgs e)
		{
		    if (e.ListChangedType == ListChangedType.ItemAdded)
		    {
		        // Set foreign key value
		        colArticles[e.NewIndex].CategoryId = CategoryId;
		    }
		}
		
		#endregion
		
			
		
		#region ForeignKey Properties
		
		/// <summary>
		/// Returns a Template ActiveRecord object related to this ArticleCategory
		/// 
		/// </summary>
		public Impact.Model.Objects.Template Template
		{
			get { return Impact.Model.Objects.Template.FetchByID(this.TemplateId); }
			set { SetColumnValue("TemplateId", value.TemplateID); }
		}
		
		
		#endregion
		
		
		
		//no ManyToMany tables defined (0)
		
        
        
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(int? varTemplateId,string varNameKey,string varName,string varDescription,bool varAllowComments,bool varAllowRss,bool varMenuVisible,string varCreatedBy,DateTime? varCreatedOn,string varModifiedBy,DateTime? varModifiedOn)
		{
			ArticleCategory item = new ArticleCategory();
			
			item.TemplateId = varTemplateId;
			
			item.NameKey = varNameKey;
			
			item.Name = varName;
			
			item.Description = varDescription;
			
			item.AllowComments = varAllowComments;
			
			item.AllowRss = varAllowRss;
			
			item.MenuVisible = varMenuVisible;
			
			item.CreatedBy = varCreatedBy;
			
			item.CreatedOn = varCreatedOn;
			
			item.ModifiedBy = varModifiedBy;
			
			item.ModifiedOn = varModifiedOn;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}
		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(int varCategoryId,int? varTemplateId,string varNameKey,string varName,string varDescription,bool varAllowComments,bool varAllowRss,bool varMenuVisible,string varCreatedBy,DateTime? varCreatedOn,string varModifiedBy,DateTime? varModifiedOn)
		{
			ArticleCategory item = new ArticleCategory();
			
				item.CategoryId = varCategoryId;
			
				item.TemplateId = varTemplateId;
			
				item.NameKey = varNameKey;
			
				item.Name = varName;
			
				item.Description = varDescription;
			
				item.AllowComments = varAllowComments;
			
				item.AllowRss = varAllowRss;
			
				item.MenuVisible = varMenuVisible;
			
				item.CreatedBy = varCreatedBy;
			
				item.CreatedOn = varCreatedOn;
			
				item.ModifiedBy = varModifiedBy;
			
				item.ModifiedOn = varModifiedOn;
			
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}
		#endregion
        
        
        
        #region Typed Columns
        
        
        public static TableSchema.TableColumn CategoryIdColumn
        {
            get { return Schema.Columns[0]; }
        }
        
        
        
        public static TableSchema.TableColumn TemplateIdColumn
        {
            get { return Schema.Columns[1]; }
        }
        
        
        
        public static TableSchema.TableColumn NameKeyColumn
        {
            get { return Schema.Columns[2]; }
        }
        
        
        
        public static TableSchema.TableColumn NameColumn
        {
            get { return Schema.Columns[3]; }
        }
        
        
        
        public static TableSchema.TableColumn DescriptionColumn
        {
            get { return Schema.Columns[4]; }
        }
        
        
        
        public static TableSchema.TableColumn AllowCommentsColumn
        {
            get { return Schema.Columns[5]; }
        }
        
        
        
        public static TableSchema.TableColumn AllowRssColumn
        {
            get { return Schema.Columns[6]; }
        }
        
        
        
        public static TableSchema.TableColumn MenuVisibleColumn
        {
            get { return Schema.Columns[7]; }
        }
        
        
        
        public static TableSchema.TableColumn CreatedByColumn
        {
            get { return Schema.Columns[8]; }
        }
        
        
        
        public static TableSchema.TableColumn CreatedOnColumn
        {
            get { return Schema.Columns[9]; }
        }
        
        
        
        public static TableSchema.TableColumn ModifiedByColumn
        {
            get { return Schema.Columns[10]; }
        }
        
        
        
        public static TableSchema.TableColumn ModifiedOnColumn
        {
            get { return Schema.Columns[11]; }
        }
        
        
        
        #endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string CategoryId = @"CategoryId";
			 public static string TemplateId = @"TemplateId";
			 public static string NameKey = @"NameKey";
			 public static string Name = @"Name";
			 public static string Description = @"Description";
			 public static string AllowComments = @"AllowComments";
			 public static string AllowRss = @"AllowRss";
			 public static string MenuVisible = @"MenuVisible";
			 public static string CreatedBy = @"CreatedBy";
			 public static string CreatedOn = @"CreatedOn";
			 public static string ModifiedBy = @"ModifiedBy";
			 public static string ModifiedOn = @"ModifiedOn";
						
		}
		#endregion
		
		#region Update PK Collections
		
        public void SetPKValues()
        {
                if (colArticles != null)
                {
                    foreach (Impact.Model.Objects.Article item in colArticles)
                    {
                        if (item.CategoryId != CategoryId)
                        {
                            item.CategoryId = CategoryId;
                        }
                    }
               }
		}
        #endregion
    
        #region Deep Save
		
        public void DeepSave()
        {
            Save();
            
                if (colArticles != null)
                {
                    colArticles.SaveAll();
               }
		}
        #endregion
	}
}
